﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.PhongThiService;
namespace eTMS.Controllers{
public class PhongThiControllerBase : CommonController{
	public PhongThiControllerBase()
		: base()
	{	
		this.dao = new PhongThiServiceClient();
	}
	~PhongThiControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private PhongThiEntity oPhongThi;	
	public PhongThiEntity PhongThiObject{
		get{
			return oPhongThi;
		}
		set{
			oPhongThi=value;
		}
	}
	protected PhongThiServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSPhongThi(false);			
			return _Table;
		}
	}
	public void LoadAllDSPhongThi(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oPhongThi==null)return false;
		bool result=true;
		try{
			oPhongThi = dao.SaveRet(oPhongThi);
		}
		catch{
			result=false;
		}
		if(result){
			_ListPhongThi.Add(oPhongThi);
		}
		return result;
	}
	public bool Update(){
		if(oPhongThi==null)return false;
		return dao.Update(oPhongThi);
	}
	public bool Delete(){
		if(oPhongThi==null)return false;
		bool retvalue = dao.Delete(oPhongThi);
		if(retvalue){			
			_ListPhongThi.Remove(oPhongThi);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 PhongThi_ID){
		bool retvalue = dao.DeleteByID(PhongThi_ID);		
		if(retvalue){
			PhongThiEntity oDeleted=FindPhongThi(PhongThi_ID);
			if(oDeleted!=null){
				_ListPhongThi.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oPhongThi = new PhongThiEntity();
	}
    //Copy data from data row in grid to object
    public PhongThiEntity MapDataRowToObject(DataRow row){
		PhongThiEntity o = new PhongThiEntity();
		 o.MaPhongThi=Convert.ToString(row["MaPhongThi"]);
		 if(!row.IsNull("MonThi_ID"))
			o.MonThi_ID=(Int32)(row["MonThi_ID"]);
		 if(!row.IsNull("PhongThi_ID"))
		    o.PhongThi_ID=(Int32)(row["PhongThi_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(PhongThiEntity o,DataRow row){
		row["MaPhongThi"]=o.MaPhongThi;
		row["MonThi_ID"]=o.MonThi_ID;
		row["PhongThi_ID"]=o.PhongThi_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.PhongThiObject,row);
    }
	public void LoadObject(Int32 PhongThi_ID){
		PhongThiEntity oLoad=FindPhongThi(PhongThi_ID);
		if(oLoad==null){
			oPhongThi=this.LoadPhongThi(PhongThi_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oPhongThi.IsNullPhongThi_ID;
			if(!bNotIsNull){
				this.ListPhongThi.Add(oPhongThi);
			}
		}
		else{
			oPhongThi=oLoad;
		}
	}
	#region CachedList PhongThi
	private static List<PhongThiEntity> _ListPhongThi=new List<PhongThiEntity>();
    public List<PhongThiEntity>ListPhongThi{
		get{
			return _ListPhongThi;
		}
	}
	public static List<PhongThiEntity>ListPhongThiStatic{
		get{
			return _ListPhongThi;
		}
	}
	public PhongThiEntity FindPhongThi(Int32 PhongThi_ID){
		PhongThiEntity oResult=this.ListPhongThi.Find(delegate(PhongThiEntity oFind){
			bool result=true; 
			result&=oFind.PhongThi_ID.Equals(PhongThi_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllPhongThi(bool clearCache){
		if(_ListPhongThi.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		PhongThiServiceClient dao = new PhongThiServiceClient();
		_ListPhongThi=new List<PhongThiEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public PhongThiEntity LoadPhongThi(Int32 PhongThi_ID){			
		PhongThiEntity o=dao.SelectOne(PhongThi_ID);		
		return o;
	}
	#endregion
}
}
